﻿


function IsNumber(number) {
    if (number == "" || isNaN(Number(number)))
        return false;
    else
        return true;
}

function GetNumber(value) {
    var result = "";
    var temp = value.split("");
    for (var i = 0; i < temp.length; i++) {
        if (IsNumber(temp[i]))
            result = result + temp[i].toString();
    }
    return result;
}

function GetCharCode(text) {
    var lchar = text.charAt(text.length - 1);
    var code = lchar.charCodeAt(0);
    return code;
}


function GetStringAddress(value) {
    var result = "";
    var code;
    var temp = value.split("");
    for (var i = 0; i < temp.length; i++) {
        //code = GetCharCode(temp[i]);
        code = temp[i].charCodeAt(0);
        if ((code >= 48 && code <= 57) || (code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code == 44 || code == 46 || code == 32)
            result = result + temp[i].toString();
    }
    return result;
}

function GetIndex(controlNotValid, controlID) {
    var arrData = controlNotValid.split(",");
    for (var i = 0; i < arrData.length; i++) {
        if (arrData[i] == controlID) {
            return i;
        }
    }
    return -1;
}

function ReplaceLeftRightEmpty(value)
{ 
    var result = ""
    try {
        var i = 0;
        while ((value[0] == " " || value[value.length - 1] == " ") && value.length > 0 && i<500) {
            if (value[0] == " ") {
                value = value.substring(1, value.length);
            }
            if (value[value.length - 1] == " ") {
                value = value.substring(0,value.length - 1);
            }
            i = i+ 1;
        }
        result = value;
    }
    catch (err) {
        result = value;
    }
    return result;
}

function IsDate(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1]; // p@rse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
       // alert("Month " + month + " doesn`t have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !isleap)) {
            //alert("February " + year + " doesn`t have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}

function IsEqualString(strValue1, strValue2) {
         var temp1 = strValue1;
         var temp2 = strValue2;
         temp1 = ReplaceLeftRightEmpty(temp1).toUpperCase();
         temp2 = ReplaceLeftRightEmpty(temp2).toUpperCase();
         if (temp1 == temp2) {
             return true;
         }
         return false;
     }

function SetReadOnlyControl(control, isReadOnly) {
    try {
        if (isReadOnly) {
            control.setAttribute("onkeypress", "return false");
        }
        else {
            control.setAttribute("onkeypress", "return true");
        }

    }
    catch (err) {
        alert(err);
    }
}

function IsEmailAddress(strEmail) {
    var emailPattern = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)/;
    //alert(strEmail.toLowerCase());
    return emailPattern.test(strEmail.toLowerCase());
}

function IsPhoneNumber(strPhone) {
    var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
    return phoneNumberPattern.test(strPhone);
}

function GetTexboxValue(txtInput) {
    var newValue = "";
    try
    {
         var strInput = txtInput.value.replace("'", "");
         for(var i =0 ; i< strInput.length; i++ )
         {
            if(strInput[i] != "'" && strInput[i] != "$" )
            {
                newValue = newValue + strInput[i];
            }
         }
     //strInput = strInput.replace("'", "");
    }
    catch(err)
    {
        newValue = strInput
    }
    return newValue;
}
